package com.gxsyh.nantie.phone.consumer.configure;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

/**
 * @email yom535@outlook.com
 * @author: 刘锋嘉(Liu Fengjia)
 * @date: 2020/1/21
 * @time: 15:06
 * @Description:
 */
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true,jsr250Enabled = true)
public class PhoneResourceServerConfiguration extends ResourceServerConfigurerAdapter {
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .exceptionHandling()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                /**
                 * 上传轮播图
                 */
                .antMatchers("/phone/update/images").hasAnyAuthority("System")
                /**
                 * 增加公告
                 */
                .antMatchers("/phone/add/information").hasAnyAuthority("System")
                /**
                 * 更新公告
                 */
                .antMatchers("/phone/update/information").hasAnyAuthority("System")
                /**
                 * 删除公告
                 */
                .antMatchers("/phone/delete/information").hasAnyAuthority("System")
                /**
                 * 置顶公告
                 */
                .antMatchers("/phone/top/information").hasAnyAuthority("System")
                /**
                 * 查询轮播图
                 */
                .antMatchers("/phone/get/all/home/images").authenticated()
                /**
                 * 查询公告
                 */
                .antMatchers("/phone/select/all/information").authenticated()
                /**
                 * 管理员删除留言
                 */
                .antMatchers("/phone/admin/delete/discuss").hasAnyAuthority("System");
    }
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        // 配置资源 ID
        resources.resourceId("backend-resources");
    }
}
